import { Component } from '@angular/core';

import { Platform, AlertController, App, List, ModalController, NavController, NavParams } from 'ionic-angular';

import { SMS } from '@ionic-native/sms';

import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser';

import { CallNumber } from '@ionic-native/call-number';

import { ContactDetailService } from './detail.service';

import { Config } from '../../../providers/config';

import { Common } from '../../../providers/common';


import { EmailSendPage } from '../../email/send/send.component';

import _ from 'lodash';

import { Storage } from '@ionic/storage';


@Component({
  selector: 'page-contact-detail',
  templateUrl: './detail.html',
  providers: [ContactDetailService]
})
export class ContactDetailPage {

  moa_page_name: any = "page-contact-detail";
  moa_page_explain: any = "人员信息";

  user: any = {};

  _ = _;

  constructor(
    public alertCtrl: AlertController,
    public app: App,
    public platform: Platform,
    public modalCtrl: ModalController,
    public navCtrl: NavController,
    public detailService: ContactDetailService,
    public navParams: NavParams,
    public storage: Storage,
    public sms: SMS,
    public sanitizer: DomSanitizer,
    public common: Common,
    public callNumber: CallNumber,
    public config: Config
  ) {
    this.user = this.navParams.data;
    if (this.user.full_orgnames) {
      this.user.full_orgnames = _.sortBy(this.user.full_orgnames, ['sort']);
    }
    if (this.user.empphoto && (_.endsWith(this.user.empphoto, 'jpeg') || _.endsWith(this.user.empphoto, 'jpg') || _.endsWith(this.user.empphoto, 'bmp') || _.endsWith(this.user.empphoto, 'gif'))) {
      this.storage.get('id_token').then((token) => {
        this.user.new_empphoto = this.config.server + "api/v1/common/img?access_token=" + token + "&url=" + this.user.empphoto + "&resize=true";
      });
    }
    console.info(this.user);
  }

  sendSMS() {
    if (this.user.empmobile) {
      if (this.platform.is("ios")) {
        // 发送短信
        this.sms.send(this.user.empmobile, '');
      }
    } else {
      let alert = this.alertCtrl.create({
        title: '提示',
        subTitle: '用户预留手机号为空',
        "buttons": ['确认']
      });
      alert.present();
    }
  }

  sanitize(url: string) {
    if (this.platform.is("android")) {
      return this.sanitizer.bypassSecurityTrustUrl(url);
    } else {
      return null;
    }
  }

  callNum() {
    if (this.user.empmobile) {
      if (this.platform.is("ios")) {
        // 拨打电话
        this.callNumber.callNumber(this.user.empmobile, true);
      }
    } else {
      let alert = this.alertCtrl.create({
        title: '提示',
        subTitle: '用户预留手机号为空',
        "buttons": ['确认']
      });
      alert.present();
    }
  }

  sendEmail() {
    var recievelist = this.common.formatContactListData([this.user]);
    var recievname = this.user.emname;
    console.info(recievelist);
    this.navCtrl.push(EmailSendPage, {
      recievelist: recievelist,
      recievname: recievname
    });
  }

  ionViewDidLoad() {
    // this.app.setTitle('ContactDetail');
  }

}
